import {
  post
} from '../../utils/request';
import {
  bMapTransqqMap
} from '../../utils/util';

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: null,

    globalData: app.globalData,

    fixedHeight: app.globalData.titleHeight + app.globalData.statusBarHeight + 45,
    fixedhd: false,

    currentTab: -1,
    currentFloor: null,
    heightList: [],

    slide: {
      indicatorDots: true,
      vertical: false,
      autoplay: true,
      interval: 2000,
      duration: 500,
    },

    detail: null,
    mchList: [],

    showMchsModal: false,
    showSpecsModal: false,

    specIdx: -1,
    specNum: 1,
  },

  goBack() {
    wx.navigateBack();
  },

  // 获取商家列表
  getMchList(inGoodsId) {
    let location = wx.getStorageSync('location') || {};
    post(`/mch/info/list`, {
        ...location,
        inGoodsId
      })
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            mchList: resp.data,
          });
        }
      });
  },

  // 获取数据
  getDetail(id) {
    post(`/mall/goods/detail/${app.globalData.tenantCode}/${id}`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            detail: resp.data,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      });
  },

  // 确定购买
  onBuy(e) {
    app.setLocation();

    let {
      idx
    } = e.currentTarget.dataset;

    app.checkLogin(true, () => {
      let spec;
      let num = this.data.specNum;
      if (idx !== undefined) {
        spec = this.data.detail.specList[idx];
        num = 1;
      } else {
        spec = this.data.detail.specList[this.data.specIdx];
      }
      wx.showLoading({
        title: '正在提交',
        mask: true
      });
      post("/order/pre-add", {
        type: 0,
        id: this.data.detail.id,
        subId: spec.id,
        num
      }).then(res => {
        wx.hideLoading();
        if (res.code === 0) {
          wx.navigateTo({
            url: `/pages/order/preview?id=${res.data.id}`,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          });
          return;
        }
      });
    });
  },

  // 选择规格
  onSelectSpec(e) {
    if (this.data.detail.status === 0 || this.data.detail.sellStatus === 0) {
      return;
    }

    let {
      idx
    } = e.currentTarget.dataset;
    let spec = this.data.detail.specList[idx];
    if (spec.status === 0 || spec.stock <= 0) {
      return;
    }

    this.setData({
      specIdx: idx,
      specNum: 1,
    });
  },

  // 加规格数量
  plusSpec() {
    if (this.data.specIdx < 0) {
      return;
    }
    this.setData({
      specNum: this.data.specNum + 1,
    });
  },

  // 减规格数量
  reduceSpec() {
    if (this.data.specNum <= 1) {
      return;
    }
    this.setData({
      specNum: this.data.specNum - 1,
    });
  },

  // 商家导肮
  onAddress(e) {
    let {
      lng,
      lat,
      address
    } = e.currentTarget.dataset;
    let local = bMapTransqqMap(lng, lat);
    wx.openLocation({
      longitude: local.longitude,
      latitude: local.latitude,
      address: address,
    });
  },

  // 联系商家
  onServiceTel(e) {
    let {
      servicetel
    } = e.currentTarget.dataset;
    if (servicetel) {
      wx.makePhoneCall({
        phoneNumber: servicetel,
      });
    }
  },

  // 适用商家模态框关闭
  closeMchsModal() {
    this.setData({
      showMchsModal: false
    });
  },

  // 适用商家模态框打开
  openMchsModal() {
    this.setData({
      showMchsModal: true
    });
  },

  // 可选规格模态框关闭
  closeSpecsModal() {
    this.setData({
      showSpecsModal: false
    });
  },

  // 可选规格模态框打开
  openSpecsModal() {
    this.setData({
      showSpecsModal: true
    });
  },

  // 首页
  onHome() {
    wx.reLaunch({
      url: '/pages/home/index',
    });
  },

  // 分享
  onShare() {

  },

  // 点击标签
  onChgTab(e) {
    this.setData({
      currentFloor: `floor${e.currentTarget.dataset.tab}`,
    });
  },

  // 滚动监听
  changeScroll(e) {
    // 获取距离顶部的距离
    let scrollTop = e.detail.scrollTop;

    if (scrollTop >= this.data.globalData.windowWidth - this.data.fixedHeight) {
      if (!this.data.fixedhd) {
        this.setData({
          fixedhd: true,
        });
      }
    } else {
      if (this.data.fixedhd) {
        this.setData({
          fixedhd: false,
          currentFloor: null,
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let options = app.getPageOptions();
    this.getDetail(options.id);
    this.getMchList(options.id);

    app.getConfig(config => {
      this.setData({
        config
      });
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.detail.name,
      path: `/pages/goods/detail?id=${this.data.detail.id}`,
      imageUrl: this.data.detail.rawAlbum[0]
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareTimeline() {
    return {
      title: this.data.detail.name,
      path: `/pages/goods/detail?id=${this.data.detail.id}`,
      imageUrl: this.data.detail.rawAlbum[0]
    }
  },

});